package cn.kongwg.demo02;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author kongwg
 * @create 2021-05-25 17:53
 */
public class Abadmeo {
    public static void main(String[] args) {
        AtomicStampedReference<Integer> atomicStampedReference =
                new AtomicStampedReference<Integer>(100, 1);
        //CopyOnWriteArrayList
        //ArrayList
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "one\t" + atomicStampedReference.getStamp());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            atomicStampedReference.compareAndSet(100, 101, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
            System.out.println(Thread.currentThread().getName() + "two\t" + atomicStampedReference.getStamp());
            atomicStampedReference.compareAndSet(101, 100, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
            System.out.println(Thread.currentThread().getName() + "three" + atomicStampedReference.getStamp());

        }, "t1").start();


        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "one" + atomicStampedReference.getStamp());
            int stamp = atomicStampedReference.getStamp();
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean b = atomicStampedReference.compareAndSet(100, 2020, stamp, stamp + 1);
            System.out.println("b = " + b);
            System.out.println(Thread.currentThread().getName() + "two" + atomicStampedReference.getStamp());
            System.out.println();
        }, "t2").start();
    }
}
